package com.framework.logAop;

import java.io.File;

import org.apache.ibatis.io.Resources;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hyperic.sigar.Sigar;

public class SigarUtils {
	public static Logger logger = LogManager.getLogger(SigarUtils.class);
	private static Sigar sigar = null;

	public static Sigar getSigar() {
		if (sigar == null) {
			sigar = initSigar();
			return sigar;
		} else {
			return sigar;
		}
	}

	private static Sigar initSigar() {
		try {
			// 此处只为得到依赖库文件的目录，可根据实际项目自定义
			File file = Resources.getResourceAsFile("/");
			File files = new File(file.getAbsolutePath() + "\\file");
			String path = System.getProperty("java.library.path");
			String sigarLibPath = files.getCanonicalPath();
			// 为防止java.library.path重复加，此处判断了一下
			if (!path.contains(sigarLibPath)) {
				boolean flag = isOSWin();
				if (flag) {
					path += ";" + sigarLibPath;
				} else {
					path += ":" + sigarLibPath;
				}
				path += ";" + sigarLibPath;
				String result = System.setProperty("java.library.path", path);
				logger.warn("警告信息" + result);
			}
			return new Sigar();
		} catch (Exception e) {
			logger.error("错误信息", e);
			return null;
		}
	}

	public static boolean isOSWin(){//OS 版本判断
        String OS = System.getProperty("os.name").toLowerCase();
        if (OS.indexOf("win") >= 0) {
            return true;
        } else return false;
    }
	
}